/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pos.system.database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Shameera
 */
public class StockDataTable extends AbstractTableModel{
    
    private ResultSet results;
    List<String[]> stockData = new ArrayList<String[]>();
//    String[] columnNames = new String[]{"Code", "Item Name", "Seller", "Qty", "Price", "Date"};
    String[] columnNames = new String[]{DBConstant.STOCK_CODE, DBConstant.STOCK_NAME , DBConstant.STOCK_SELLER,
                            DBConstant.STOCK_QTY , DBConstant.STOCK_UNIT_PRICE , DBConstant.STOCK_DATE};

    public StockDataTable(ResultSet results) {
        setTable(results);
    }

    public void setTable(ResultSet res) {
        this.results = res;
        try {
            String[] temp;
            while (res.next()) {
                temp = new String[columnNames.length];
                for(int i =0 ; i < columnNames.length; i++){
                    temp[i] = (res.getString(columnNames[i])==null? "" : res.getString(columnNames[i]));
                }
                stockData.add(temp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(StockDataTable.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public int getRowCount() {
        return stockData.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return stockData.get(rowIndex)[columnIndex];
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }
    
}
